package com.itany.corejava.code07_面向对象;

/**
 * @author 石小俊
 * @date 2023年04月18日 10:17
 */
public class Test07_构造方法 {
    public static void main(String[] args) {
        User user = new User();
        System.out.println("id:" + user.id + ",username:" + user.username + ",password:" + user.password);
        User user2 = new User(1);
        System.out.println("id:" + user2.id + ",username:" + user2.username + ",password:" + user2.password);
        User user3 = new User("admin", "123456");
        System.out.println("id:" + user3.id + ",username:" + user3.username + ",password:" + user3.password);
        User user4 = new User(2, "alice", "123456");
        System.out.println("id:" + user4.id + ",username:" + user4.username + ",password:" + user4.password);
    }
}

class User {
    int id;
    String username;
    String password;

    public User() {
        System.out.println("调用无参构造方法");
    }

    public User(int id) {
        this.id = id;
    }

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public User(int id, String username, String password) {
        // this()只能调用一次,且必须处于第一行
        this(username, password);
        this.id = id;
    }
}

